﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace CPPEI.Coolzon.UI.DataPartLib
{
    /// <summary>
    /// 表格的列
    /// </summary>
    public class DataGridRow : DependencyObject
    {
        private DataGridRowUpdateDirection _flipDirection { get; set; }

        public List<DataGridCell> Cells { get; set; }

        public List<DataGridCell> OldCells { get; set; }

        public int RowIndex { get; private set; }
        public double Height { get; set; }

        private bool _isReplaceMode = false;

        Storyboard _flipStoryboard;

        public double FlipValue
        {
            get
            {
                return (double)GetValue(FlipValueProperty);
            }
            set
            {
                SetValue(FlipValueProperty, value);
            }
        }

        public static DependencyProperty FlipValueProperty = DependencyProperty.Register(
            "FlipValue",
            typeof(double),
            typeof(DataGridRow),
            new PropertyMetadata(default(double), OnFlipValuePropertyChangedCallback));

        public DataGridRow(int rowIndex, DataGridRowUpdateDirection updateDirection)
        {
            Cells = new List<DataGridCell>();
            OldCells = new List<DataGridCell>();

            RowIndex = rowIndex;

            this._flipDirection = updateDirection;
        }

        public void AddCell(DataGridCell cell)
        {
            Cells.Add(cell);
        }

        public void ReplaceCell(DataGridCell cell, int cellIndex)
        {
            OldCells.Add(Cells[cellIndex]);

            Cells[cellIndex] = cell;
        }

        internal void Remove(Grid container)
        {
            if (_flipStoryboard != null)
            {
                _flipStoryboard.Stop();
                _flipStoryboard.Remove();
                _flipStoryboard = null;
            }

            if (OldCells.Count > 0)
            {
                for (int index = 0; index < OldCells.Count; index++)
                {
                    DataGridCell cell = OldCells[index];
                    cell.Deactive();
                    _container.Children.Remove(cell);
                }
                OldCells.Clear();
            }
            if (Cells.Count > 0)
            {
                for (int index = 0; index < Cells.Count; index++)
                {
                    DataGridCell cell = Cells[index];
                    cell.Deactive();
                    _container.Children.Remove(cell);
                }
                Cells.Clear();
            }
        }

        Grid _container;
        /// <summary>
        /// 附加到表格容器上
        /// </summary>
        /// <param name="container"></param>
        internal void AttackToGrid(Grid container, int headerRowCount, double beginTime, double dataAnimationDuration, double rowLoopDuration)
        {
            if (_flipStoryboard != null)
            {
                _flipStoryboard.Stop();
                _flipStoryboard.Remove();
                _flipStoryboard = null;
            }

            _container = container;
            if (OldCells.Count > 0)
            {
                _flipStoryboard = new Storyboard();
                _flipStoryboard.Completed += _flipStoryboard_Completed;
                DoubleAnimation animation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(rowLoopDuration)));
                animation.BeginTime = TimeSpan.FromSeconds(beginTime);

                Storyboard.SetTarget(animation, this);
                Storyboard.SetTargetProperty(animation, new PropertyPath(DataGridRow.FlipValueProperty));
                _flipStoryboard.Children.Add(animation);

                for (int index = 0; index < OldCells.Count; index++)
                {
                    UIElement cell = OldCells[index];
                    //(cell as Border).BorderBrush = new SolidColorBrush(Colors.Blue);
                    //(cell as Border).BorderThickness = new Thickness(2);
                    ScaleTransform scaleTransform = new ScaleTransform();
                    scaleTransform.ScaleY = 1;
                    scaleTransform.ScaleX = 1;
                    if (this._flipDirection == DataGridRowUpdateDirection.Down)
                    {
                        cell.RenderTransformOrigin = new Point(0.5, 1);
                    }
                    else
                    {
                        cell.RenderTransformOrigin = new Point(0.5, 0);
                    }
                    cell.RenderTransform = scaleTransform;
                }

                for (int index = 0; index < Cells.Count; index++)
                {
                    UIElement cell = Cells[index];
                    //(cell as Border).BorderBrush = new SolidColorBrush(Colors.Red);
                    //(cell as Border).BorderThickness = new Thickness(2);
                    Grid.SetColumn(cell, index);
                    Grid.SetRow(cell, headerRowCount + RowIndex);
                    container.Children.Add(cell);

                    ScaleTransform scaleTransform = new ScaleTransform();
                    scaleTransform.ScaleY = 0;
                    scaleTransform.ScaleX = 1;
                    if (this._flipDirection == DataGridRowUpdateDirection.Down)
                    {
                        cell.RenderTransformOrigin = new Point(0.5, 0);
                    }
                    else
                    {
                        cell.RenderTransformOrigin = new Point(0.5, 1);
                    }
                    cell.RenderTransform = scaleTransform;
                }
            }
            else
            {
                // 第一次加载数据时的动画
                _flipStoryboard = new Storyboard();
                _flipStoryboard.Completed += _flipStoryboard_Completed;
                DoubleAnimation animation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(dataAnimationDuration)));
                animation.BeginTime = TimeSpan.FromSeconds(RowIndex * dataAnimationDuration);

                Storyboard.SetTarget(animation, this);
                Storyboard.SetTargetProperty(animation, new PropertyPath(DataGridRow.FlipValueProperty));
                _flipStoryboard.Children.Add(animation);

                for (int index = 0; index < Cells.Count; index++)
                {
                    UIElement cell = Cells[index];
                    //(cell as Border).BorderBrush = new SolidColorBrush(Colors.Red);
                    //(cell as Border).BorderThickness = new Thickness(2);
                    Grid.SetColumn(cell, index);
                    Grid.SetRow(cell, headerRowCount + RowIndex);
                    container.Children.Add(cell);
                    cell.Opacity = 0.0d;

                    //ScaleTransform scaleTransform = new ScaleTransform();
                    //scaleTransform.ScaleY = 0;
                    //scaleTransform.ScaleX = 1;
                    //if (this._flipDirection == DataGridRowUpdateDirection.Down)
                    //{
                    //    cell.RenderTransformOrigin = new Point(0.5, 0);
                    //}
                    //else
                    //{
                    //    cell.RenderTransformOrigin = new Point(0.5, 1);
                    //}
                    //cell.RenderTransform = scaleTransform;
                }
            }

            if (_flipStoryboard != null)
            {
                _flipStoryboard.Begin();
            }
        }

        void _flipStoryboard_Completed(object sender, EventArgs e)
        {
            ClockGroup clock = sender as ClockGroup;
            if (clock.CurrentState == ClockState.Filling)
            {
                if (OldCells.Count > 0)
                {
                    for (int index = 0; index < OldCells.Count; index++)
                    {
                        DataGridCell cell = OldCells[index];
                        cell.Deactive();
                        _container.Children.Remove(cell);
                    }
                    OldCells.Clear();

                    for (int index = 0; index < Cells.Count; index++)
                    {
                        UIElement cell = Cells[index];
                        ScaleTransform scaleTransform = cell.RenderTransform as ScaleTransform;
                        scaleTransform.ScaleY = 1;
                    }
                }
            }
        }

        public static void OnFlipValuePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataGridRow row = d as DataGridRow;
            double fv = (double)e.NewValue;

            if (row.OldCells != null && row.OldCells.Count > 0)
            {
                for (int index = 0; index < row.OldCells.Count; index++)
                {
                    UIElement cell = row.OldCells[index];
                    ScaleTransform scaleTransform = cell.RenderTransform as ScaleTransform;
                    scaleTransform.ScaleY = (1 - fv);
                }

                for (int index = 0; index < row.Cells.Count; index++)
                {
                    UIElement cell = row.Cells[index];
                    ScaleTransform scaleTransform = cell.RenderTransform as ScaleTransform;
                    scaleTransform.ScaleY = fv;
                }
            }
            else
            {

                for (int index = 0; index < row.Cells.Count; index++)
                {
                    UIElement cell = row.Cells[index];
                    cell.Opacity = fv;
                }
            }
        }
    }
}
